package cn.ailanyin.home.module.things.service.impl;

import cn.ailanyin.home.common.context.UserInfoContext;
import cn.ailanyin.home.common.exception.ServiceException;
import cn.ailanyin.home.common.utils.EntityUtil;
import cn.ailanyin.home.common.utils.VOUtil;
import cn.ailanyin.home.module.category.entity.Category;
import cn.ailanyin.home.module.category.mapper.CategoryMapper;
import cn.ailanyin.home.module.cupboard.entity.Cupboard;
import cn.ailanyin.home.module.cupboard.mapper.CupboardMapper;
import cn.ailanyin.home.module.things.dto.ThingsDTO;
import cn.ailanyin.home.module.things.entity.Things;
import cn.ailanyin.home.module.things.mapper.ThingsMapper;
import cn.ailanyin.home.module.things.service.IThingsService;
import cn.ailanyin.home.module.things.vo.ThingsVO;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 小怪兽
 * @version 1.0
 * @date 2024/4/13 0013 20:31:59
 */

@Service
@RequiredArgsConstructor
public class ThingsServiceImpl implements IThingsService {

    /**
     * 事物映射器
     */
    private final ThingsMapper thingsMapper;

    /**
     * 分类映射器
     */
    private final CategoryMapper categoryMapper;

    /**
     * 橱柜映射器
     */
    private final CupboardMapper cupboardMapper;

    @Override
    public List<ThingsVO> list(ThingsDTO dto) {
        List<ThingsVO> vos = new ArrayList<>();
        LambdaQueryWrapper<Things> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getName()), Things::getName, dto.getName())
                .eq(Things::getHomeId, UserInfoContext.getHomeId());
        List<Things> list = thingsMapper.selectList(wrapper);
        for (Things things : list) {
            String categoryName = "";
            if (StrUtil.isNotBlank(things.getCategoryId())) {
                Category category = categoryMapper.selectById(things.getCategoryId());
                categoryName = category.getName();
            }
            String location = "";
            if (StrUtil.isNotBlank(things.getCupboardId())) {
                Cupboard cupboard = cupboardMapper.selectById(things.getCupboardId());
                location = cupboard.getLocation();
            }
            ThingsVO vo = VOUtil.getThingsVO(things, categoryName, location);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public void saveOrUpdate(ThingsDTO dto) {
        if (StrUtil.isBlank(dto.getName())) {
            throw new ServiceException("物品名不可为空");
        }
        if (StrUtil.isBlank(dto.getCupboardId())) {
            throw new ServiceException("橱柜位置不可为空");
        }
        if (dto.getCount() == null) {
            throw new ServiceException("物品数量不可为空");
        }
        if (StrUtil.isBlank(dto.getId())) {
            Things things = EntityUtil.getThings(dto);
            thingsMapper.insert(things);
            return;
        }
        LambdaUpdateWrapper<Things> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(StrUtil.isNotBlank(dto.getName()), Things::getName, dto.getName())
                .set(Objects.nonNull(dto.getCount()), Things::getCount, dto.getCount())
                .set(StrUtil.isNotBlank(dto.getImgUrls()), Things::getImgUrls, dto.getImgUrls())
                .set(StrUtil.isNotBlank(dto.getCategoryId()), Things::getCategoryId, dto.getCategoryId())
                .set(StrUtil.isNotBlank(dto.getCupboardId()), Things::getCupboardId, dto.getCupboardId())
                .eq(Things::getId, dto.getId())
                .eq(Things::getHomeId, UserInfoContext.getHomeId());
        thingsMapper.update(wrapper);
    }

    @Override
    public void delete(String id) {
        LambdaQueryWrapper<Things> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Things::getHomeId, UserInfoContext.getHomeId())
                .eq(Things::getId, id);
        thingsMapper.delete(wrapper);
    }


}
